package com.yuanchu.goods.controller;

import com.yuanchu.common.exception.BizCodeEnum;
import com.yuanchu.common.utils.PageUtils;
import com.yuanchu.common.utils.R;
import com.yuanchu.common.validator.ValidationGroups;
import com.yuanchu.goods.dto.*;
import com.yuanchu.goods.entity.YoshopGoodsEntity;
import com.yuanchu.goods.service.YoshopGoodsService;
import com.yuanchu.goods.service.YoshopGoodsSkuService;
import com.yuanchu.goods.utils.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;


/**
 * 商品记录表
 *
 * @author adan
 * @email 2028038953@gmail.com
 * @date 2023-12-19 02:17:43
 */
@Api(tags = "商品基本信息接口", description = "YoshopGoodsController")
@RestController
// @RequestMapping()
public class YoshopGoodsController {
    @Resource
    private YoshopGoodsService yoshopGoodsService;

    @Resource
    private YoshopGoodsSkuService goodsSkuService;

    /**
     * 查询商品列表
     */
    @ApiOperation("查询商品列表接口")
    @GetMapping("/list")
    public R list(QueryGoodsParamsDto params) {
        if (params == null) {
            params = new QueryGoodsParamsDto();
        }
        PageUtils page = yoshopGoodsService.queryGoodsList(params);
        // List<YoshopGoodsEntity> list = yoshopGoodsService.queryGoodsALl(params);
        HashMap<String, Object> map = new HashMap<>();
        map.put("list", page);
        return R.ok().put("data", map);
    }


    /**
     * 根据id查询商品信息
     */
    @ApiOperation("根据id查询商品详细信息")
    @GetMapping("/detail")
    public R queryGoodsDetail(@RequestParam("goodsId") Integer goodsId) {
        QueryGoodsDetail yoshopGoods = yoshopGoodsService.queryGoodsDetailById(goodsId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("goodsInfo", yoshopGoods);
        return R.ok().put("data", map);
    }

    @ApiOperation("根据id查询商品详细信息")
    @GetMapping("/store/detail")
    public R queryStoreGoodsDetail(@RequestParam("goodsId") Integer goodsId) {
        Integer userId = SecurityUtil.getShopUserId();

        QueryStoreGoodsDetail storeGoodsDetail = yoshopGoodsService.queryStoreGoodsDetailById(goodsId, userId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("detail", storeGoodsDetail);
        return R.ok().put("data", map);
    }

    /**
     * 添加商品
     */
    @ApiOperation("添加商品")
    @PostMapping("/add")
    public R save(@RequestBody EditGoodsParamsDto goodsParams) {
        int storeId = 10001;
        yoshopGoodsService.saveGoodsInfo(goodsParams.getForm(), storeId);

        return R.ok("添加成功");
    }

    /**
     * 修改
     */
    @ApiOperation("修改商品")
    @PostMapping("/edit")
    public R edit(@RequestBody EditGoodsParamsDto editGoodsParams) {
        yoshopGoodsService.editById(editGoodsParams, 10001);
        return R.ok("更新成功");
    }

    /**
     * 删除
     */
    @ApiOperation("删除商品")
    @PostMapping("/delete")
    public R delete(@RequestBody @Validated({ValidationGroups.Delete.class}) EditGoodsStateDto params) {
        boolean flag = yoshopGoodsService.removeGoodsByIds(params.getGoodsIds());
        if (!flag) {
            return R.error(BizCodeEnum.PRODUCT_DOWN_EXCEPTION.getCode(), BizCodeEnum.PRODUCT_DOWN_EXCEPTION.getMsg());
        }
        return R.ok();
    }

    @ApiOperation("商品上架/下架")
    @PostMapping("/state")
    public R state(@RequestBody @Validated({ValidationGroups.Edit.class}) EditGoodsStateDto params) {
        yoshopGoodsService.updateGoodsState(params);
        return R.ok();
    }

    @ApiOperation("商品总数")
    @GetMapping("/total")
    public long queryTotalGoods() {
        return yoshopGoodsService.queryTotalGoods();
    }

    @ApiOperation("已售罄商品")
    @GetMapping("/soldout")
    public long querySoldOutGoodsTotal() {
        return yoshopGoodsService.querySoldOutGoodsTotal();
    }

    @PostMapping("/media/{mediaId}")
    public boolean queryMedia(@RequestBody List<Long> fileIds) {
        return yoshopGoodsService.getGoodsByFileIds(fileIds);
    }

    @GetMapping("/store/{categoryId}")
    public List<YoshopGoodsEntity> queryGoodsByCategoryId(@PathVariable("categoryId") Integer categoryId) {
        return yoshopGoodsService.queryGoodsByCategoryId(categoryId);
    }

    @ApiOperation("根据id查询商品购物车基本信息")
    @GetMapping("/cart")
    public YoshopGoodsEntity queryGoodsInfo(@RequestParam("goodsId") Integer goodsId, @RequestParam("goodsSkuId") String goodsSkuId) {
        return yoshopGoodsService.queryGoodsInfo(goodsId, goodsSkuId);
    }

    @ApiOperation("根据id查询商品基本信息")
    @GetMapping("/base")
    public YoshopGoodsEntity queryGoodsBase(@RequestParam("goodsId") Integer goodsId) {
        return yoshopGoodsService.getById(goodsId);
    }

    @ApiOperation("根据id查询商品文件信息")
    @GetMapping("/image")
    public Integer queryGoodsImage(@RequestParam("goodsId") Integer goodsId) {
        return yoshopGoodsService.queryGoodsImage(goodsId).getImageId();
    }

    @ApiOperation("根据id查询商品库存信息")
    @GetMapping("/sku/num")
    public Integer queryGoodsStockNum(@RequestParam("goodsId") Integer goodsId, @RequestParam("goodsSkuId") String goodsSkuId) {
        return yoshopGoodsService.queryGoodsStockNum(goodsId, goodsSkuId);
    }

    @PostMapping("/stock/decr")
    public R decrStockNum(Integer goodsId, String goodsSkuId, Integer num) {
        boolean flag = goodsSkuService.decrStockNum(goodsId, goodsSkuId, num);

        if (!flag) {
            return R.error(BizCodeEnum.DECR_STOCKNUM_EXCEPTION.getCode(), BizCodeEnum.DECR_STOCKNUM_EXCEPTION.getMsg());
        }
        return R.ok();
    }

    @GetMapping("/sale/list")
    public List<YoshopGoodsEntity> querySaleGoodsList() {
        return yoshopGoodsService.querySaleGoodsList();
    }

    @RequestMapping("/goods/name")
    public String queryGoodsName(@RequestParam("goodsId") Integer goodsId){
        return yoshopGoodsService.getById(goodsId).getGoodsName();
    }
    @GetMapping("/goods/dto")
    public QueryGoodsList queryGoodsDto(@RequestParam("categoryId") Integer goodsId){
        return yoshopGoodsService.queryGoodsDto(goodsId);
    }


}
